﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ProjectManager.Masters.Forms;
using ProjectManager.Masters.Library;
using ProjectManager.Masters.ModuleData;

namespace ProjectManager.Masters.Scrounger
{
    internal class SessionsScrounger : ApplicationLibrary.DBManager.Scrounger
    {
        private UserTabControl<TabSessions> UtcSessions = null;
        private NotifyForm notifyForm = null;

        public SessionsScrounger(iScrounger frm)
            : base(frm)
        {
        }

        public override void InitializerComponent()
        {
            UtcSessions = new UserTabControl<TabSessions>(Properties.Resources.C_Sessions);
            UtcSessions.Name = "UtcSessions";
            ((iDocument)guestForm).masterTabControl.Controls.Add(UtcSessions);

            base.InitializerComponent();
        }

        public override void OnCustomizeToolbar(ToolStrip toolstrip)
        {
            guestForm.AddCustomButton("Session", Properties.Resources.C_Sessions, Properties.Resources.B_Session, true);
            base.OnCustomizeToolbar(toolstrip);
        }

        public override bool OnEnableToolbarButtons(ToolStripButton sender, DBManager dbManager)
        {
            if (sender.Name == "Session")
                return dbManager.Count > 0;

            return base.OnEnableToolbarButtons(sender, dbManager);
        }

        public override void OnCustomButtonClick(ToolStripButton button)
        {
            notifyForm             = new NotifyForm();
            notifyForm.DockAreas   = WinFormsUI.Docking.DockAreas.Float;
            notifyForm.FatherType  = ((iDocument)guestForm).ProjectType;
            notifyForm.Father      = ((iDocument)guestForm).ProjectNumb;
            notifyForm.Description = ((iDocument)guestForm).Description;

            //notifyForm.MdiParent = Application.OpenForms["frmMain"];
            if (button.Name == "Session")
                notifyForm.Show(Application.OpenForms["frmMain"]);

            base.OnCustomButtonClick(button);
        }

        public override void OnAttachData(DBManager dbManager)
        {
            dbManager.AddSlave("SESSIONSLAVE", false);
            UtcSessions.UserControl.OnAttachData(dbManager);

            base.OnAttachData(dbManager);
        }

        public override void OnPrepareAuxData()
        {
            UtcSessions.UserControl.OnPrepareAuxData();

            base.OnPrepareAuxData();
        }

        public override void OnBindData()
        {
            UtcSessions.UserControl.OnBindData(guestForm);
            base.OnBindData();
        }

        public override string CreateSlaveQuery(string name)
        {
            // Project
            //select * from pm_session
            //where type = 1 and code = 3
            //union
            //select S.* from pm_session S
            //inner join pm_project X
            //on S.type = x.type and s.code = x.code
            //inner join pm_project Y
            //on y.type = x.fathertype and y.code = x.father
            //where y.fathertype = 1 and y.father = 3;

            QueryBuilder qb = new QueryBuilder();
            if (name == "SESSIONSLAVE")
            {
                QueryBuilder qb1 = new QueryBuilder();
                qb1.SetQualified = false;

                qb.AddSelect();
                qb.AddFrom(PM_Session.Name);
                qb.AddCompare(PM_Session.Type, "@p3");
                qb.AddCompare(PM_Session.Code, "@p4");

                qb1.AddManualQuery(@"select S.* from {0} S inner join {1} X
                                    ON S.{2} = X.{3} and S.{4} = X.{5}
                                    inner join {6} Y
                                    on Y.{7} = X.{8} and Y.{9} = X.{10}",
                                        PM_Session.Name,
                                        PM_Project.Name,
                                        PM_Session.Type.Name,
                                        PM_Project.Type.Name,
                                        PM_Session.Code.Name,
                                        PM_Project.Code.Name,
                                        PM_Project.Name,
                                        PM_Project.Type.Name,
                                        PM_Project.FatherType.Name,
                                        PM_Project.Code.Name,
                                        PM_Project.Father.Name);
                qb1.AddCompare(string.Format("Y.{0}", PM_Project.FatherType.Name), "@p3", "=");
                qb1.AddCompare(string.Format("Y.{0}", PM_Project.Father.Name), "@p4", "=");
                qb1.AND_OR = "OR";
                qb1.AddCompare(string.Format("X.{0}", PM_Project.FatherType.Name), "@p3", "=");
                qb1.AND_OR = "AND";
                qb1.AddCompare(string.Format("X.{0}", PM_Project.Father.Name), "@p4", "=");

                qb1.AddOrder(PM_Session.Type);
                qb1.AddOrder(PM_Session.Code);
                qb1.AddOrder(PM_Session.ID);

                return string.Concat(qb.Query, " UNION ", qb1.Query);
            }
            return qb.Query;
        }

        public override List<SqlABParameter> CreateSlaveParam(string name)
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();
            if (name == "SESSIONSLAVE")
            {
                SqlABParameter p5 = new SqlABParameter("@p3", PM_Session.Type);
                p5.Value = PM_Session.Type.DefaultValue;
                PList.Add(p5);
                SqlABParameter p6 = new SqlABParameter("@p4", PM_Session.Code);
                p6.Value = PM_Session.Code.DefaultValue;
                PList.Add(p6);
            }
            return PList;
        }

        public override void SetParameters(DBManager dbManager, IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == "SESSIONSLAVE")
            {
                collection.Parameter[0].Value = key[0];
                collection.Parameter[1].Value = key[1];
            }
            base.SetParameters(dbManager, key, collection);
        }
    }
}